<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class m_santri extends CI_Model {

    var $santri_id;
    var $santri_nama;
    var $santri_kelamin;
    var $santri_tempat_lahir;
    var $santri_tanggal_lahir;
    var $santri_nama_ayah;
    var $santri_sekolah;
    var $santri_asal_sekolah;
    var $santri_alamat;
    var $santri_tahun;
    var $santri_status;
    var $santri_tanggal;
    var $santri_oleh;
    var $santri_dirubah;
    var $cari;
    var $paginate;
    var $num_pages;
    var $offset;

    function __construct() {
        parent::__construct();
        $this->load->library('auth');
    }

    public function get_row_all() {
        if ($this->cari) {
            $this->db->where('(santri_nama LIKE ' . $this->db->escape('%' . $this->cari . '%') . 'OR santri_alamat LIKE ' . $this->db->escape('%' . $this->cari . '%') . ')');
        }

        $query = $this->db->count_all_results('t_santri');

        return $query;
    }

    public function get() {
        if ($this->cari) {
            $this->db->where('(santri_nama LIKE ' . $this->db->escape('%' . $this->cari . '%') . 'OR santri_alamat LIKE ' . $this->db->escape('%' . $this->cari . '%') . ')');
        }

        if ($this->paginate == TRUE) {
            $query = $this->db->get('t_santri', $this->num_pages, $this->offset);
            return $query;
        }

        $query = $this->db->get('t_santri');
        return $query;
    }

    public function get_by_id($id = '0') {
        $this->db->where('santri_id', $id);
        $query = $this->db->get('t_santri');

        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return redirect('homes/');
        }
    }

    public function get_santri_name($name) {
        $this->db->select('santri_nama');
        $this->db->where('santri_nama', $name);
        $query = $this->db->get('t_santri');

        if ($query->num_rows()) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function get_kategori_menu() {
        //$this->db->select('santri_id, santri_nama', 'santri_sekolah', 'santri_kelamin');
        $this->db->where('santri_status', '1');
        $this->db->order_by('santri_id', 'ASC');
        $query = $this->db->get('t_santri');
        $result = $query->result();

        $kategori_menu = array();
        $pilihan[''] = 'Pilih';

        foreach ($result AS $row) {
            //$pilihan[$row->santri_id] = (($row->santri_kelamin == '1') ? '<i class="fa fa-male"></i>' : '<i class="fa fa-female"></i>' ) . $row->santri_nama . ' (' . $row->santri_id . ')';
            $pilihan[$row->santri_id] = $row->santri_nama . ' (' . $row->santri_id . ') - ' . $row->santri_nama_ayah . ' - ' .  $this->auth->_sekolah_santri($row->santri_sekolah);
        }
        return $pilihan;
    }

    public function save() {
        if ($this->santri_nama) {
            $db_array['santri_nama'] = $this->santri_nama;
        }

        if ($this->santri_kelamin) {
            $db_array['santri_kelamin'] = $this->santri_kelamin;
        }

        if ($this->santri_tempat_lahir) {
            $db_array['santri_tempat_lahir'] = $this->santri_tempat_lahir;
        }

        if ($this->santri_tanggal_lahir) {
            $db_array['santri_tanggal_lahir'] = $this->santri_tanggal_lahir;
        }

        if ($this->santri_nama_ayah) {
            $db_array['santri_nama_ayah'] = $this->santri_nama_ayah;
        }

        if ($this->santri_asal_sekolah) {
            $db_array['santri_asal_sekolah'] = $this->santri_asal_sekolah;
        }

        if ($this->santri_tahun) {
            $db_array['santri_tahun'] = $this->santri_tahun;
        }

        if ($this->santri_sekolah) {
            $db_array['santri_sekolah'] = $this->santri_sekolah;
        }

        if ($this->santri_alamat) {
            $db_array['santri_alamat'] = $this->santri_alamat;
        }

        if ($this->santri_status) {
            $db_array['santri_status'] = $this->santri_status;
        }

        if ($this->santri_tanggal) {
            $db_array['santri_tanggal'] = $this->santri_tanggal;
        }

        if ($this->santri_oleh) {
            $db_array['santri_oleh'] = $this->santri_oleh;
        }

        if ($this->santri_dirubah) {
            $db_array['santri_dirubah'] = $this->santri_dirubah;
        }

        if ($this->santri_id) {
            $this->db->where('santri_id', $this->santri_id);
            $this->db->update('t_santri', $db_array);
        } else {
            $this->db->insert('t_santri', $db_array);
        }
    }

    public function delete() {
        $this->db->where('santri_id', $this->santri_id);
        $this->db->delete('t_santri');
    }

}

/* End of file m_santri.php */
/* Location: ./application/model/m_santri.php */